package com.mazegame.frontend;


import com.mazegame.backend.BackgroundPanel;
import com.mazegame.test.MazePlot;
import com.mazegame.test.MazePlot2;
import com.mazegame.test.MazePlot3;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import javax.swing.*;
import java.awt.*;


public class MazeGeneratePanel extends JPanel implements GamePanel {
    private static CardLayout cardLayout;
    private static JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;
    public static boolean isFirst = true;
    public static int num;

    public MazeGeneratePanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);
        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/黑色背景.png");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);
        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层
        Platform.runLater(() -> {
            // 创建主容器
            BorderPane root = update();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

//    private BorderPane createContentPane() {
//        BorderPane root = new BorderPane();
//        javafx.scene.control.Label tip = new Label("点击任意位置继续...");
//        tip.setFont(Font.font("KaiTi", FontWeight.BOLD, 48));
//        tip.setTextFill(Color.WHITE);
//        root.setOnMouseClicked(event -> {
//            if(isFirst){
//                new com.sun.j3d.utils.applet.MainFrame(new MazePlot(), 1500, 900);
//                isFirst = false;
//            }
//        });
//        root.setCenter(tip);
//        return root;
//    }

    public static BorderPane update() {
        BorderPane root = new BorderPane();
        javafx.scene.control.Label tip = new Label("点击任意位置继续...");
        tip.setFont(Font.font("KaiTi", FontWeight.BOLD, 48));
        tip.setTextFill(Color.WHITE);
        root.setOnMouseClicked(event -> {
            if(isFirst){
                if(num==1){
                    new com.sun.j3d.utils.applet.MainFrame(new MazePlot(), 1500, 900);
                    isFirst = false;
                } else if (num==2) {
                    new com.sun.j3d.utils.applet.MainFrame(new MazePlot2(), 1500, 900);
                    isFirst = false;
                } else if (num==3) {
                    new com.sun.j3d.utils.applet.MainFrame(new MazePlot3(), 1500, 900);
                    isFirst = false;
                }
                isFirst = false;
            }
        });
        root.setCenter(tip);
        return root;
    }

    @Override
    public void showPanel() { cardLayout.show(cardPanel, "MAZEGENERATEPANEL");}

    @Override
    public void cleanup() {

    }
}
